local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();
local WeaponDropInfoGui=UIManager:CreateUI("WeaponDropInfoGui",require(script.WeaponDropInfoGui))
local WeaponDropEffectGui = UIManager:CreateUI("WeaponDropEffectGUi",require(script.WeaponDropEffectGui))
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local WeaponTool=_G.CommonOHOH.load.WeaponTool();
local Battle_Cfg=_G.CommonOHOH.config.Chapter.chapter2level
local event_list =ClientOHOH.event_list

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.Delete={}

	self.player=self.parent
    WeaponDropEffectGui:Hide();
    self.parent.BattleSateEvent:Connect(function(isBattle,battle)
        WeaponDropInfoGui:Hide()
        self:Fire(event_list.delete_data,self.Delete)

    end)
    self.Delete={}
end;

function M.dtor(self)
	M.super.dtor(self);
end;


function M.CreateCellData(self,weight,weapon)
    


    local showPercent=GeneralTool.GetProbabilityShowPercent(weight)

    local list={}
    for k,wid in pairs(weapon) do
       local art= WeaponTool.GetWeaponArtByCid(wid)
       table.insert(list,{
            weight=showPercent[k],
            id=wid,
            icon=art.Image,
            isDelete=self.Delete[wid];
            name= WeaponTool.GetNamebyCid(wid),
            DeleteEvent=_G.CommonOHOH.load.Connect().new(),
            dtor=function(this)
                this.DeleteEvent:dtor()
            end
       })
    end

    return list
end

function M.PushDelete(self,id)

    if  self.Delete[id] then
        self.Delete[id]=nil
    else

        self.Delete[id]=true
    end
end



--计算luck值
function M.GetLuckyValue(self)

	local baseList= self.player.LuckyValueBaseEvent:Action()
	local mulList=  self.player.LuckyValueMulEvent:Action()
	local addList=  self.player.LuckyValueAddEvent:Action()

	local base=0
	for _,v in pairs(baseList) do
		 base=base+v
	end
 
	local mul=1
	for _,v in pairs(mulList) do
		 mul=mul+v
	end

	local add=0
	for _,v in pairs(addList) do
		  add=add+v
	 end
	
	 local v=base*mul+add

 	return v
end

function M.Clear(self)
    if self.ListCell then
        for _,cell in pairs(self.ListCell) do
            cell:dtor()
        end
        self.ListCell={}
    end

end



function M.ShowLuckyInfo(self,battleId)
    self.id2Index={}
    self:Clear()

    local cfg= Battle_Cfg[battleId]
    self.ListCell=self:CreateCellData(cfg.weight,cfg.weapon)

    for k,cell in pairs(self.ListCell) do
        cell.DeleteEvent:Connect(function()
             self:PushDelete(cell.id)
        end)
        self.id2Index[cell.id]=k
    end
    _G.ClientOHOH.load.UIManager().ui_list.HomeGui:HideTop()
    WeaponDropInfoGui:Show(self.ListCell,self:GetLuckyValue())


end;

function M.CloseLuckyInfo(self)
    _G.ClientOHOH.load.UIManager().ui_list.HomeGui:ShowTop()
    WeaponDropInfoGui:Hide()
end;

function M.DropWeapon(self,wid)

   local index= self.id2Index[wid]
    local cell=self.ListCell[index]
    local icon =  cell.icon;
    local percentText =math.floor(cell.weight).."%";
    local name =cell.name;
    WeaponDropEffectGui:OnShow(icon,name,percentText);

end

M:RegistEvent(event_list.weapon_drop,M.DropWeapon)


return M
